package com.itheima.netty.c8;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;

public class Server {
    public static void main(String[] args) {
        try {
            NioEventLoopGroup boss = new NioEventLoopGroup();
            NioEventLoopGroup worker = new NioEventLoopGroup();
            new ServerBootstrap()
                    .group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new LoggingHandler());
                            // ch.pipeline().addLast(new StringDecoder());
                            // ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            //     @Override
                            //     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            //         ByteBuf in = (ByteBuf) msg;
                            //         try {
                            //             while (in.isReadable()) {
                            //                 System.out.print((char) in.readByte());
                            //                 System.out.flush();
                            //             }
                            //         } finally {
                            //             ReferenceCountUtil.release(msg);
                            //         }
                            //         // super.channelRead(ctx, msg);
                            //     }
                            // });
                            ch.pipeline().addLast(new TimeServerHandler());
                        }
                    })
                    .bind(8080);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
